﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace DarwinIsDead.Bullets {
    /// <summary>
    /// A Bullet that moves in a straight line.  Just use this if you're making
    /// a standard gun thing.
    /// </summary>
    public class Linear : Bullet {
        // Movement speed;
        private float speed;
        private float range;
        private float distance;

        // Color
        private Color color;
        // Sprite
        private Texture2D Sprite { get; set; }

        public Linear(World world, int damage, float speed, float range, bool friendly)
            : base(world, 16f, 16f, friendly, damage) {
            Sprite = World.Game.Content.Load<Texture2D>("Sprites\\Bullets\\sprTestBullet");
            color = Color.White;

            this.speed = speed;
            this.range = range;
        }

        public Linear(World world, Rectangle rect, bool friendly, int damage,
            float speed, float range)
            : base(world, rect, friendly, damage) {
                initSprite(world.Game.GraphicsDevice);

                distance = 0f;
                this.speed = speed;
                this.range = range;
        }

        public Linear(World world, Vector2 size, bool friendly, int damage,
            float speed, float range)
            : base(world, size, friendly, damage) {
                initSprite(world.Game.GraphicsDevice);

                distance = 0f;
                this.speed = speed;
                this.range = range;
        }

        public Linear(World world, float w, float h, bool friendly, int damage,
            float speed, float range)
            : base(world, w, h, friendly, damage) {
            initSprite(world.Game.GraphicsDevice);

            distance = 0f;
            this.speed = speed;
            this.range = range;
        }

        private void initSprite(GraphicsDevice graphicsDevice) {
            // Create a rectangle with a solid color, displayed if a sprite
            // hasn't been set
            Sprite = new Texture2D(graphicsDevice, HitBox.Width,
                HitBox.Height);
            Color[] data = new Color[(int)(HitBox.Width * HitBox.Height)];
            Color col = Friendly ? Color.Blue : Color.Red;

            for (int i = 0; i < data.Length; i++) {
                data[i] = col;
            }

            Sprite.SetData(data);
        }

        public override void Fire() {
            // Use this method to play a firing sound unique to this bullet,
            // or if needed, to set the Solid property.  That last bit is VERY
            // important in order for this to hit anything
            Solid = true;
        }

        public override void Collision(Entity other, Rectangle overlap) {
            // You can use this to determine how much a penetration power a
            // bullet has, among other things.  This one stops as soon as it
            // hits something.
            if ((!Friendly && other is Player) ||
                (Friendly && other is Zombie)) {
                Active = false;
                distance = 0f;
                if (NotifyDamage != null) {
                    NotifyDamage(Damage);
                }
            }
        }

        protected override void InternalUpdate(GameTime gameTime) {
            // Delta time
            float dt = gameTime.ElapsedGameTime.Milliseconds / 1000f;

            // Trigonometry
            float dx = dt * speed * (float)Math.Cos(FireAngle);
            float dy = dt * speed * (float)Math.Sin(FireAngle);
            float dp = (float)Math.Sqrt(dx*dx + dy*dy);

            // Move
            Move(dx, dy);

            // Handle dissipation over distance
            distance += dp;
            if (distance > range) {
                Active = false;
                distance = 0f;
            }
        }

        protected override void InternalDraw(SpriteBatch spriteBatch) {
            spriteBatch.Draw(Sprite, Position, color);
        }
    }
}
